package com.wyh.controller.gfive;

import com.google.protobuf.ServiceException;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.common.result.Result;
import com.wyh.common.utils.ServletUtils;
import com.wyh.common.utils.StringUtils;
import com.wyh.domain.dto.gfive.LoginBody;
import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.po.gfive.User;
import com.wyh.security.gfive.utils.SecurityUtils;
import com.wyh.service.gfive.SysLoginService;
import com.wyh.service.gfive.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@RestController
@Api(tags = "登录")
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private UserService userService;


    /*
     * 账号密码登录
     * */

    @PostMapping("/login")
    @ApiOperation("账号密码登录")
    public Result login(@RequestBody @Valid LoginBody loginBody) {

        String token = loginService.login(loginBody.getAccount(), loginBody.getPassword());
        return Result.success(token);
    }

    /*
     * 验证码登录
     * */
    @PostMapping("/captchaLogin")
    @ApiOperation("验证码登录")
    public Result loginCaptcha(@RequestBody @Valid LoginBody loginBody) {

        String token = loginService.loginCaptcha(loginBody.getTelephone(), loginBody.getCaptcha());
        return Result.success(token);
    }

    /*
     * 获取验证码
     */
    @PostMapping("/captcha")
    @ApiOperation("获取验证码")
    public Result captcha(@RequestBody @Valid LoginBody loginBody)  {
        String phone = loginBody.getTelephone();
        return loginService.sendCaptcha(phone);
    }


    /*
     * 修改密码
     */
    @PostMapping("/updatePwd")
    @ApiOperation("修改密码")
    public Result updatePwd(@RequestBody Map<String, String> params) throws ServiceException {
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        return loginService.updatePassword(oldPwd, newPwd, rePwd);
    }

    /*
    * 获取用户信息
    * */

    @GetMapping("/getLoginUser")
    @ApiOperation("获取用户信息")
    public Result getLoginUser() throws ServiceException {
        LoginUser loginUser = SecurityUtils.getLoginUser();
         if(StringUtils.isNull(loginUser)){
             throw new CustomException(ResultCodeEnum.USER_NOT_LOGIN);
         }
        User user = loginUser.getUser();
         user.setPassword(null);
        return Result.success(user);
    }



    //权限验证测试
    @PostMapping("/findByUsername")
    @ApiOperation("权限验证测试")
    @PreAuthorize("@ss.hasPermi('note:invoice')")
    public Result byUsername(@RequestBody User user) {

        User byUsername = userService.findByUsername(user.getAccount());
        return Result.success(byUsername);
    }

    //权限验证测试
    @PostMapping("/selectUserTelephoneById")
    @ApiOperation("权限验证测试")
    @PreAuthorize("@ss.hasRole('客户')")
    public Result userPhone(@RequestBody User user) {

        String byUsername = userService.selectUserTelephoneById(user.getId());
        return Result.success(byUsername);
    }


}
